<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中..."
      border
      fit
      highlight-current-row
      class="custom-table"
      :row-class-name="tableRowClassName"
      style="width: 100%;"
      :header-cell-style="{ 'text-align': 'center', 'vertical-align': 'middle' }"
      :cell-style="{ 'text-align': 'center', 'vertical-align': 'middle' }"
    >
      <!-- 序号 -->
      <el-table-column label="序号" align="center" width="80">
        <template #default="scope">
          {{ (current - 1) * size + scope.$index + 1 }}
        </template>
      </el-table-column>

      <!-- 图片 -->
      <el-table-column label="图片" align="center" width="280">
        <template #default="scope">
          <el-image
            style="width: 260px; height: 145px; border-radius: 12px; box-shadow: 0 6px 14px rgba(0,0,0,0.15);"
            :src="scope.row.imageUrl"
            :preview-src-list="[scope.row.imageUrl]"
            fit="cover"
            lazy
          />
        </template>
      </el-table-column>

      <!-- 标题 -->
      <el-table-column prop="title" label="标题" align="center" min-width="180"/>

      <!-- 链接地址 -->
      <el-table-column prop="linkUrl" label="链接地址" align="center" min-width="220">
        <template #default="scope">
          <a :href="scope.row.linkUrl" target="_blank" rel="noopener noreferrer" style="color: #409EFF;">
            {{ scope.row.linkUrl }}
          </a>
        </template>
      </el-table-column>

      <!-- 添加时间 -->
      <el-table-column
        prop="gmtCreate"
        label="添加时间"
        align="center"
        min-width="220"
        :formatter="formatDate"
      />

      <!-- 操作 -->
      <el-table-column label="操作" align="center" width="220">
        <template #default="scope">
          <router-link :to="'/banner/save/' + scope.row.id">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              class="action-btn"
            >修改
            </el-button>
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            class="action-btn"
            @click="removeDataById(scope.row.id)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="current"
      :page-size="size"
      :total="total"
      layout="total, prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      class="pagination"
      background
    />
  </div>
</template>

<script>
import bannerApi from '@/api/edu/banner'

export default {
  data() {
    return {
      listLoading: false,
      list: [],
      total: 0,
      current: 1,
      size: 10
    }
  },

  created() {
    this.getList()
  },

  methods: {
    getList(page = 1) {
      this.current = page
      this.listLoading = true

      bannerApi.getBannerListPage(this.current, this.size)
        .then(response => {
          if (response.success) {
            this.list = response.data.records || []
            this.total = response.data.total || 0
          } else {
            this.list = []
            this.total = 0
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
          this.list = []
          this.total = 0
        })
        .finally(() => {
          this.listLoading = false
        })
    },

    handleCurrentChange(val) {
      this.getList(val)
    },

    removeDataById(id) {
      this.$confirm('确定要删除该轮播图吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        bannerApi.deleteBannerById(id).then(response => {
          if (response.success) {
            this.$message.success('删除成功')
            this.getList(this.current)
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },

    formatDate(row, column, cellValue) {
      if (!cellValue) return ''
      const date = new Date(cellValue)
      return date.toLocaleString()
    },

    tableRowClassName({ rowIndex }) {
      return rowIndex % 2 === 1 ? 'odd-row' : ''
    }
  }
}
</script>

<style scoped>
.app-container {
  width: 100%;
  height: 100vh;
  padding: 30px 40px;
  background-color: #fff;
  box-sizing: border-box;
  min-height: 85vh;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

.custom-table ::v-deep(.el-table__body-wrapper) {
  max-height: 580px;
  overflow-y: auto;
}

.odd-row {
  background-color: #f7f9fc;
}

.action-btn {
  margin: 0 6px;
  transition: background-color 0.3s ease;
}

.action-btn:hover {
  opacity: 0.85;
  cursor: pointer;
}

.pagination {
  margin-top: 40px;
  text-align: center;
  user-select: none;
  font-weight: 600;
  font-size: 15px;
  color: #606266;
}
</style>
